{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a0f212fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyspark import SparkContext, SparkConf\n",
    "import findspark\n",
    "findspark.init()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "68c9c36b",
   "metadata": {},
   "outputs": [],
   "source": [
    "conf = SparkConf().setAppName(\"demo\")\n",
    "sc = SparkContext(conf=conf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6c7cc3f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "rdd = sc.parallelize([1,2,3,4,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f03c6296",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "print(rdd.collect())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5c543b6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ParallelCollectionRDD[0] at readRDDFromFile at PythonRDD.scala:287\n"
     ]
    }
   ],
   "source": [
    "print(rdd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "1dddf207",
   "metadata": {},
   "outputs": [],
   "source": [
    "rdd2 = sc.textFile(\"file:///D:/text.txt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "5b7bb2ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hello world', 'hello spark', 'hello python']\n"
     ]
    }
   ],
   "source": [
    "print(rdd2.collect())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "badcbe33",
   "metadata": {},
   "outputs": [],
   "source": [
    "# rdd2.saveAsTextFile(\"file:///D:/2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e99e9053",
   "metadata": {},
   "outputs": [],
   "source": [
    "# map, filter, reduce\n",
    "rdd3 = sc.parallelize(range(100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "69622f20",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109]\n"
     ]
    }
   ],
   "source": [
    "rdd4 = rdd3.map(lambda x: x+10)\n",
    "print(rdd4.collect())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "a4e6d060",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]\n"
     ]
    }
   ],
   "source": [
    "rdd5 = rdd3.filter(lambda x: x % 2 == 0 )\n",
    "print(rdd5.collect())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b78ee9d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4950\n"
     ]
    }
   ],
   "source": [
    "result = rdd3.reduce(lambda x, y: x+y)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "aa21389c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hello', 'world', 'hello', 'spark', 'hello', 'python']\n"
     ]
    }
   ],
   "source": [
    "#  flatMap, reduceByKey\n",
    "rdd6 = rdd2.flatMap(lambda x: x.split(\" \"))\n",
    "print(rdd6.collect())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cca5532a",
   "metadata": {},
   "outputs": [],
   "source": [
    "(a,b)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
